<template>
  <div class="app-container">
    <el-tooltip
      class="item"
      effect="dark"
      :content="$t('merchantManagement.createTip')"
      placement="top-start"
    >
      <el-button
        class="addBtn"
        type="primary"
        icon="el-icon-plus"
        @click="addMerchant"
        >{{ $t("merchantManagement.create") }}</el-button
      >
    </el-tooltip>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline sp_between"
    >
      <div>
        <el-form-item :label="$t('merchantManagement.account')">
          <el-input v-model="formInline.account" />
        </el-form-item>
        <el-form-item :label="$t('merchantManagement.email')">
          <el-input v-model="formInline.email" />
        </el-form-item>
      </div>
      <el-form-item>
        <el-button
          plain
          type="primary"
          icon="el-icon-search"
          @click="onSubmit"
          >{{ $t("orderForm.search") }}</el-button
        >
      </el-form-item>
    </el-form>
    <div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
      >
        <el-table-column
          show-overflow-tooltip
          prop="account"
          :label="$t('merchantTable.account')"
        />
        <el-table-column
          show-overflow-tooltip
          prop="name"
          :label="$t('merchantTable.merchantName')"
        />
        <el-table-column :label="$t('merchantTable.name')">
          <template slot-scope="scope"
            >{{ scope.row.lastName }}.{{ scope.row.firstName }}</template
          >
        </el-table-column>

        <el-table-column
          show-overflow-tooltip
          prop="email"
          :label="$t('merchantTable.email')"
        />
        <el-table-column
          prop="role"
          show-overflow-tooltip
          :label="$t('merchantTable.role')"
        />
        <el-table-column prop="status" :label="$t('merchantTable.status')">
          <template slot-scope="scope">{{
            scope.row.status
              ? $t("merchantTable.locked")
              : $t("merchantTable.unlocked")
          }}</template>
        </el-table-column>
        <el-table-column
          prop="operationTime"
          :label="$t('merchantTable.operationTime')"
          width="180"
        >
          <template slot-scope="scope">{{
            timeWord(scope.row.operationTime)
          }}</template>
        </el-table-column>
        <el-table-column fixed="right" :label="$t('merchantTable.actions')">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.status"
              style="color: #fb0000; width: 80px; text-align: left"
              size="mini"
              type="text"
              @click="lockedUser(scope.row)"
              >{{
                scope.row.status
                  ? $t("merchantTable.unlocked")
                  : $t("merchantTable.locked")
              }}</el-button
            >
            <el-tooltip
              v-else
              class="item"
              effect="dark"
              :content="$t('merchantTable.lockedTip')"
              placement="top-start"
            >
              <el-button
                style="color: #fb0000; width: 80px; text-align: left"
                size="mini"
                type="text"
                @click="lockedUser(scope.row)"
                >{{
                  scope.row.status
                    ? $t("merchantTable.unlocked")
                    : $t("merchantTable.locked")
                }}</el-button
              >
            </el-tooltip>
            <el-button size="mini" type="text" @click="editGroup(scope.row)">{{
              $t("merchantTable.edit")
            }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block cz_center">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="formInline.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
        <span class="pageText"> {{ $t("pagination.page") }} </span>
      </div>
    </div>
    <!-- 添加用户 -->
    <el-dialog
      width="900px"
      :title="titleForm"
      :visible.sync="dialogFormVisible"
      @close="resetForm('rulesForm')"
    >
      <el-form
        ref="rulesForm"
        :rules="rulesForm"
        class="createGroup"
        :model="form"
        label-width="260px"
      >
        <el-form-item
          prop="name"
          :label="$t('merchantManagement.formName')"
          required
        >
          <el-input
            v-model="form.name"
            :placeholder="$t('merchantManagement.formNameTip')"
          />
        </el-form-item>
        <el-form-item
          prop="account"
          :label="$t('merchantManagement.formAccount')"
          required
        >
          <el-input
            v-model="form.account"
            :disabled="titleForm === this.$t('merchantManagement.editTitle')"
            :placeholder="$t('merchantManagement.formAccountTip')"
          />
        </el-form-item>
        <el-form-item
          :label="$t('merchantManagement.formManagerName')"
          required
        >
          <el-form-item style="margin-bottom: 22px" prop="firstName">
            <el-input
              v-model="form.firstName"
              :placeholder="$t('merchantManagement.formManagerNameTip1')"
            />
          </el-form-item>
          <el-form-item prop="lastName">
            <el-input
              v-model="form.lastName"
              :placeholder="$t('merchantManagement.formManagerNameTip2')"
            />
          </el-form-item>
        </el-form-item>
        <el-form-item
          v-if="titleForm === 'Create merchant'"
          prop="password"
          :label="$t('merchantManagement.formPassword')"
          required
        >
          <el-input name="hidden" class="hidden" type="text" />
          <el-input
            name="hidden1"
            class="hidden1"
            type="password"
            show-password
          />
          <el-input
            autocomplete="off"
            auto-complete="new-password"
            v-model="form.password"
            show-password
            type="password"
          />
          <div class="tipTxt">
            {{ $t("merchantManagement.formPasswordTip") }}
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addProductGroup('rulesForm')">{{
          $t("merchantManagement.confirm")
        }}</el-button>

        <el-button @click="resetForm('rulesForm')">{{
          $t("merchantManagement.cancel")
        }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getMerchantManagements,
  setMerchantManagement,
  addMerchantManagement,
  getMerchantManagementById,
} from "@/api/merchantManagement";
export default {
  name: "List",
  data() {
    var validateName = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("The merchant name cannot be empty"));
      } else {
        let zz = /^[a-zA-Z0-9@\$\^\.\*\\?\_\-]{2,30}$/;
        console.log("=====", zz.test(value));
        if (!zz.test(value)) {
          return callback(new Error("Merchant name format error"));
        }
        return callback();
      }
    };
    var validateAccount = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("The account cannot be empty"));
      } else {
        let zz = /^[a-zA-Z0-9@\$\^\.\*\\?\_\-]{6,}$/;
        console.log("=====", zz.test(value));
        if (!zz.test(value)) {
          return callback(new Error("Account format error"));
        }
        return callback();
      }
    };
    return {
      form: {},
      dialogFormVisible: false,
      titleForm: this.$t("merchantManagement.addTitle"),
      rulesForm: {
        name: [
          {
            validator: validateName,
            trigger: "blur",
          },
        ],
        account: [
          {
            validator: validateAccount,
            trigger: "blur",
          },
        ],
        firstName: [
          {
            required: true,
            message: this.$t("rulesManagement.firstName"),
            trigger: "blur",
          },
        ],
        lastName: [
          {
            required: true,
            message: this.$t("rulesManagement.lastName"),
            trigger: "blur",
          },
        ],
        password: [
          {
            required: true,
            message: this.$t("rulesManagement.password"),
            trigger: "blur",
          },
        ],
      },

      currentPage: 1,
      total: 0,
      formInline: {
        page: 1,
        limit: 10,
      },
      tableData: [],
    };
  },
  created() {
    this.init();
  },
  methods: {
    editGroup(row) {
      this.titleForm = this.$t("merchantManagement.editTitle");
      getMerchantManagementById(row).then((response) => {
        this.form = response.data;
        this.dialogFormVisible = true;
      });
    },
    resetForm(formName) {
      this.form = {};
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    },
    addProductGroup(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.merchantName = this.form.firstName + this.form.lastName;
          addMerchantManagement(this.form).then((response) => {
            this.$message({
              message: this.$t("merchantManagement.success"),
              type: "success",
            });
            this.init();
            this.dialogFormVisible = false;
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    addMerchant() {
      this.titleForm = this.$t("merchantManagement.addTitle");
      this.form.password = "00000000";
      this.dialogFormVisible = true;
    },
    lockedUser(row) {
      let tip;
      if (!row.status) {
        tip = this.$t("rulesManagement.nolock");
      } else {
        tip = this.$t("rulesManagement.lock");
      }
      this.$confirm(tip, this.$t("rulesManagement.tip"), {
        confirmButtonText: this.$t("rulesManagement.confirm"),
        cancelButtonText: this.$t("rulesManagement.cancel"),
        type: "warning",
      })
        .then(async () => {
          await setMerchantManagement(row);
          this.$message({
            message: "success",
            type: "success",
          });
          this.init();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("rulesManagement.canceled"),
          });
        });
    },
    seeDetail(id) {
      this.$router.push({
        path: "details?id=" + id,
      });
    },
    handleSizeChange(val) {
      this.formInline.limit = val;
      this.init();
    },
    handleCurrentChange(val) {
      this.formInline.page = val;
      this.init();
    },
    init() {
      getMerchantManagements(this.formInline).then((response) => {
        console.log(response);
        this.tableData = response.data;
        this.storage.set("shopers", response.data);
        this.total = response.count;
      });
      this.storageUsers();
    },
    onSubmit() {
      console.log(this.formInline);
      this.formInline.page = 1;
      this.formInline.limit = 10;
      this.init();
    },
    storageUsers() {
      getMerchantManagements({
        page: 1,
        limit: 999,
      }).then((response) => {
        this.storage.set("shopers", response.data);
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.hidden,
.hidden1 {
  opacity: 0;
  position: fixed;
  left: 99999999999999px;
}
.oldPrice span {
  color: #c0c4cc !important;
  text-decoration: line-through !important;
}
.createGroup {
  .el-input,
  .el-select {
    width: 500px;
  }
}
.addBtn {
  margin-bottom: 12px;
}
.tipTxt {
  font-size: 12px;
  color: #b0b0b0;
}
</style>
